package com.client.service;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Author e4glet
 * 2025年3月5日
 * */

// 客户端核心模块
public class OICQClient {
    private static final String SERVER_ADDRESS = "localhost";
    private static final int SERVER_PORT = 12345;
    private Socket socket;
    // 接收服务器端传过来的数据
    BufferedReader reader;  // 唯一读取器
    // 发送给服务器端的数据
    private PrintWriter writer;
    private int userId; // 添加userId变量

    // 构造函数中初始化socket、writer、scanner
    public OICQClient() {
        try {
            socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
            //初始化socket输出流
            writer = new PrintWriter(socket.getOutputStream(), true);
            //初始化socket输入流
            InputStream input = socket.getInputStream();
            reader = new BufferedReader(new InputStreamReader(input));
            System.out.println("已连接到服务器");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 注册用户
    public boolean registerUser(String email, String nickname, String password) throws IOException {
        writer.println("REGISTER " + email + " " + nickname + " " + password);
        String response = reader.readLine();
        return "REGISTER_SUCCESS".equals(response);
    }

    // 登录用户
    public boolean loginUser(String email, String password) throws IOException {
        writer.println("LOGIN " + email + " " + password);
        String response = reader.readLine();
        if ("LOGIN_SUCCESS".equals(response)) {
            this.userId = Integer.parseInt(reader.readLine());
            return true;
        }
        return false;
    }

    // 添加getter方法
    public int getUserId() {
        return userId;
    }

    // 添加发送消息方法
    public boolean addFriend(int userId, int friendId) throws IOException {
        writer.println("ADD_FRIEND " + userId + " " + friendId);
        String response = reader.readLine();
        if ("FRIEND_ALREADY_EXISTS".equals(response)) {
            throw new IOException("Friend already exists");
        }
        return "ADD_FRIEND_SUCCESS".equals(response);
    }

    // 添加搜索用户方法
    public String searchUserByEmail(String email) throws IOException {
        writer.println("SEARCH_USER " + email);
        String response = reader.readLine();
        if (response != null && response.startsWith("SEARCH_USER_SUCCESS")) {
            return response;
        }
        return null;
    }

    // 添加发送消息方法
    public void sendMessage(int friendId, String message) throws IOException {
        // 调用writer向服务器端发送消息
        writer.println("SEND_MESSAGE " + userId + " " + friendId + " " + message);
        System.out.println("客户端成功发送消息！");
    }


    // 添加获取好友列表方法
    public String getFriendList() throws IOException {
        writer.println("GET_FRIENDS " + userId);
        System.out.println("客户端请求用户id：" + userId);
        return reader.readLine();
    }

    // 添加登出方法
    public void logout() throws IOException {
        try {
            writer.println("LOGOUT " + userId);
            writer.flush();
            socket.close();
            System.out.println("客户端退出成功.");
        } finally {
            if (socket != null && !socket.isClosed()) {
                socket.close();
            }
        }
    }

    /**
     * 获取历史消息
     * */
    public String getMessages(int userId, int friendId) throws IOException {
        writer.println("GET_MESSAGES " + userId + " " + friendId);
        return reader.readLine();
    }

    // 添加关闭方法
    public void close() {
        try {
            if (writer != null) writer.close();
            if (reader != null) reader.close();
            if (socket != null && !socket.isClosed()) socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
